package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.ASDU;
import com.hongxin.nw.netty.model.analysis.info.ExDTmspna1;
import com.hongxin.nw.netty.mq_info.domain.CMD104;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.ModifyJsonKeyUtil;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.MqIdentif;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @remark  交流, 充电中状态信息104
 * @author: GuoXiaolin
 * @date 2019/6/21 19:24
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class ExChargingStatus implements IMq {
    @SuppressWarnings("unchecked")
    @Override
    public JSONObject sendMqInfo(Transfer transfer)  throws Exception{

        //json字段
        String jsonRemoveWorkingState= "workingState";//业务系统不需要的字段

        ASDU asdu = transfer.getAsdu();
        String code = transfer.getCode();
        List<ExDTmspna1> myl = (List<ExDTmspna1>) transfer.getObj();
        List<Integer> states = new ArrayList<>();
        for (int i = 0; i < asdu.getVsq(); i++){
            ExDTmspna1 dataItem = myl.get(i);
            if (StaticValue.QPS.equals(dataItem.getQds())){//只发送qds == 1 的情况
                if (dataItem.getInfoAdr_i()!=0&&dataItem.getInfoAdr_i()!=4){
                    states.add(getMqNeeded(dataItem.getInfoAdr_i()));//将状态存字符串数组
                }
            }
        }
        CMD104 cmd104 = new CMD104();
        cmd104.setInterfaceCount(String.valueOf(myl.get(0).getGun()+1));//充电口号
        cmd104.setMachineId(code);//充电桩编码,ASCII编码
        cmd104.setInterfaceNumber(AsduStartPools.getAsduStart(code).getNum());//充电枪数量
        cmd104.setInterfaceType(TypeIdentif.E_TYPE_COMMUNICATION.getStr());//充电枪类型,2=直流； 1=交流；
        cmd104.setStateCount(String.valueOf(myl.size()));
        cmd104.setStates(states.toString());

        JSONObject json = (JSONObject) JSONObject.toJSON(cmd104);
        json.put(Catalogue.HXCMD, Catalogue.CMD104);
        JSONObject mqJson = ModifyJsonKeyUtil.removeEmptyKey(json);
        mqJson.remove(jsonRemoveWorkingState);

//        if (!isSendMq(states)){
//            return JSONUtils.getRoot(-1,"只含503,515中任意1,2个或告警信息为空时,不发送mq", StaticValue.VAL_NULL);
//        }
//        if (states.size() == 0){
//            return JSONUtils.getRoot(-1,"交流告警信息为空,不发送MQ", StaticValue.VAL_NULL);
//        }
        return JSONUtils.getRoot(0, StaticValue.VAL_26, mqJson);
    }
    private Integer getMqNeeded(int i){
        Integer str = 0;
        switch (i){
            case 0 :
                str = MqIdentif.REPORT_503.getInt();//"连接确认开关状态";*****
                break;
            case 1:
                str = MqIdentif.REPORT_507.getInt();//"交流输入过压告警";
                break;
            case 2:
                str = MqIdentif.REPORT_508.getInt();//"交流输入欠压告警";
                break;
            case 3:
                str = MqIdentif.REPORT_70.getInt();//"充电电流过负荷告警";
                break;
            case 4:
                str = MqIdentif.REPORT_515.getInt();//"输出继电器状态";*****
                break;
            case 5:
                str = MqIdentif.REPORT_527.getInt();//"温度过高保护";
                break;
            case 6:
                str = MqIdentif.REPORT_422.getInt();//"短路保护";
                break;
            case 7:
                str = MqIdentif.REPORT_208.getInt();//"漏电保护";
                break;
            case 8:
                str = MqIdentif.REPORT_504.getInt();//"急停开关触发";
                break;
        }
        return str;

    }

    private boolean isSendMq(List<Integer> states) {
        int g1 = MqIdentif.REPORT_503.getInt();
        int g2 = MqIdentif.REPORT_515.getInt();
        if (states.isEmpty()){//警告信息为空不发送
            return false;
        }
        if (states.size()==1&&states.contains(g1)){//警告信息元素为1,且包含503,则不发送
            return false;
        }
        if (states.size()==1&&states.contains(g2)){//警告信息元素为1,且包含515,则不发送
            return false;
        }
        if (states.size()==2&&states.contains(g2)&&states.contains(g1)){//警告信息元素为2,且包含515和503,则不发送
            return false;
        }
        return true;
    }
}
